<script>
import axios from "axios";

export default {
  name: "Article",
  data() {
    return {
      markdownOption: {},
      tableData: [],
      title: "",
      content: null,
      currentPage: 1,
      pageSize: 5,
      total: 0,
      loading: true,
      multipleSelection: [],
      form: {},
      formLabelWidth: '100px',
      dialogFormVisible1: false,
      dialogFormVisible: false,
      dialogViewVisible: false,
      id: null,
      ids: [],
      user:localStorage.getItem("user")? JSON.parse(localStorage.getItem("user")):{}
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/article/selectAll", {
        params: {
          title: this.title,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.loading = false;
        this.total = res.data.total;

      });
    },
    imgAdd(pos, $file) {
      let $vm = this.$refs.md;
      let formData = new FormData();
      formData.append('file', $file);
      axios({
        url: this.baseUrl+'/file/upload',
        method: 'post',
        data: formData,
        headers: {'Content-Type': 'multipart/form-data'}
      }).then((res) => {
        console.log(res);
        $vm.$img2Url(pos, res.data.data);
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.title = "";
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleDelete(id) {
      this.request.delete("/article/delete/" + id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      this.request.post("/article/deleteBatch", ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleEdit(row) {
      this.form = row;
      this.id = row.id;
      this.dialogFormVisible1 = true;
    },
    edit() {
      this.request.post("/article/edit/" + this.id, this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '文章修改成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible1 = false;
          this.form = {};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    handleSave(){
      this.form={};
      this.dialogFormVisible=true;
    },
    save() {
      this.request.post("/article/save", this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '文章创建成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible = false;
          this.form = {};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    view(content) {
      console.log(content);
      this.content=content;
      this.dialogViewVisible = true;
    },
    headerBg({row,rowIndex}){
      if(rowIndex==0){
        return 'headerBgClass'
      }
    }
  }
}
</script>

<template>
  <div>
    <div style="margin:10px 0px">
      <el-input style="width:160px" v-model="title" suffix-icon="el-icon-search" placeholder="请输入名称"></el-input>
      <el-button type="primary" class="ml-5" @click="load">搜索</el-button>
      <el-button type="warning" class="ml-5" @click="reset">重置</el-button>
    </div>
    <div style="margin:10px 0px">
      <el-button type="primary" class="ml-5" icon="el-icon-circle-plus-outline" @click="handleSave">新增
      </el-button>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" class="ml-5" icon="el-icon-remove-outline" slot="reference" v-if="user.role=='ROLE_ADMIN'">批量删除</el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData"
              border stripe
              v-loading="loading"
              max-height="550"
              :header-cell-class-name="headerBg"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255,225,255, 0.1)"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55px"></el-table-column>
      <el-table-column prop="id" label="ID" width="55px">
      </el-table-column>
      <el-table-column prop="title" label="文章标题">
      </el-table-column>
      <el-table-column  label="文章内容">
        <template slot-scope="scope">
          <el-button   type="primary" plain @click="view(scope.row.content)">查看内容</el-button>
        </template>

      </el-table-column>
      <el-table-column prop="publisher" label="发布者">
      </el-table-column>
      <el-table-column prop="pubdate" label="发布时间">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.row.id)"
          >
            <el-button type="danger" icon="el-icon-delete" slot="reference" v-if="user.role=='ROLE_ADMIN'">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding:10px 0px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增文章" :visible.sync="dialogFormVisible" width="60%" center>
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="文章标题">
          <el-input v-model="form.title" autocomplete="off" clearable></el-input>
        </el-form-item>
        <div class="sketch_content">
        <el-form-item label="文章内容">
          <mavon-editor ref="md" style="height: 390px" v-model="form.content" :ishljs="true"  @imgAdd="imgAdd"/>
        </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="文章信息" :visible.sync="dialogViewVisible" width="60%" >
      <el-card shadow="hover">
        <div>
          <mavon-editor
              class="md"
              :value="content"
              :subfield="false"
              :defaultOpen="'preview'"
              :toolbarsFlag="false"
              :editable="false"
              :scrollStyle="true"
              :ishljs="true"
              style="height: 390px"
          />
        </div>
      </el-card>
    </el-dialog>
    <el-dialog title="编辑文章" :visible.sync="dialogFormVisible1" width="60%"  center>
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="文章标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="文章内容" :label-width="formLabelWidth">
          <mavon-editor ref="md" style="height: 390px"   v-model="form.content" :ishljs="true" @imgAdd="imgAdd"></mavon-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<style>
/*
.sketch_content {
  overflow: auto;
  height: 400px;
  border-top: 1px solid rgb(151, 163, 174);
  border-bottom: 1px solid rgb(151, 163, 174);
  padding: 0px 30px 11px 27px;
}
.sketch_content::-webkit-scrollbar {
  width: 3px;
}
.sketch_content::-webkit-scrollbar-thumb {
  background: #8798AF;
  border-radius: 2px;
}
.sketch_content::-webkit-scrollbar-track {
  background: transparent;
}
*/
.headerBgClass{
  background-color: #cccccc !important;
}

</style>
